<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\agent\controller;

class AdminController extends \app\AgentRest
{
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        if($this->_user["role_name"] != "admin") {
            echo json_encode(["code" => 401, "error" => lang("Permission denied")]);
            exit;
        }
    }
    public function list()
    {
        $param = $this->_param;
        $dis = [];
        if(!empty($param["name"])) {
            $dis[] = ["a.account", "like", "%" . $param["name"] . "%"];
            $dis[] = ["d.mini_app_name", "like", "%" . $param["name"] . "%"];
        }
        $list = \app\agent\model\AdminModel::alias("a")->field(["a.admin_id", "a.level", "a.account", "a.role_id", "a.create_time", "r.description", "d.mini_app_name as mini_name", "c.modular_id"])->leftJoin("longbing_role r", "a.role_id = r.role_id")->leftJoin("longbing_app_admin c", "a.admin_id = c.admin_id")->leftJoin("longbing_card_config d", "c.modular_id = d.uniacid")->where([["a.status", "=", 1], ["a.uniacid", "=", $this->_uniacid]])->where(function ($query) {
            $query->whereOr($dis);
        })->order("a.create_time desc")->paginate(["list_rows" => $param["page_count"] ? $param["page_count"] : 10, "page" => $param["page"] ? $param["page"] : 1])->toArray();
        foreach ($list["data"] as $k => $v) {
            $list["data"][$k]["is_bind"] = !empty($v["mini_name"]);
            if($v["description"] != "超级管理员") {
                $list["data"][$k]["description"] = $v["level"] == 0 ? "管理员" : "员工";
            }
        }
        $list["zhihuituike"] = longbingIsZhihuituike();
        return $this->success($list);
    }
    public function addSubAdmin()
    {
        $input = $this->_input;
        $validate = new \app\agent\validate\AgentAdminValidate();
        if(!$validate->scene("addSubAdmin")->check($input)) {
            return $this->error($validate->getError());
        }
        $subAdmin = \app\agent\model\AdminModel::where([["account", "=", $input["account"]], ["status", "=", 1]])->findOrEmpty();
        if(!$subAdmin->isEmpty()) {
            return $this->error("该账号已存在");
        }
        $offset = createOffset();
        $new = ["admin_id" => uuid(), "account" => $input["account"], "uniacid" => $this->_uniacid, "offset" => $offset, "passwd" => createPasswd($input["passwd"], $offset), "role_id" => "e7d81116997011e99b985595a87cbdcb", "creator_id" => $this->_user["admin_id"], "status" => 1, "level" => $input["level"]];
        $rst = $subAdmin->save($new);
        if(!$rst) {
            return $this->error("fail");
        }
        return $this->success($rst);
    }
    public function updateSubAdmin()
    {
        $input = $this->_input;
        $validate = new \app\agent\validate\AgentAdminValidate();
        if(!$validate->scene("addSubAdmin")->check($input)) {
            return $this->error($validate->getError());
        }
        $subAdmin = \app\agent\model\AdminModel::where([["admin_id", "=", $input["admin_id"]], ["uniacid", "=", $this->_uniacid]])->find();
        if(empty($subAdmin)) {
            return $this->error("用户不存在");
        }
        $input["passwd"] = createPasswd($input["passwd"], $subAdmin["offset"]);
        $result = $subAdmin->save(["passwd" => $input["passwd"], "level" => $input["level"]]);
        return $this->success($result);
    }
    public function delSubAdmin()
    {
        $input = $this->_input;
        $validate = new \app\agent\validate\AgentAdminValidate();
        if(!$validate->scene("delSubAdmin")->check($input)) {
            return $this->error($validate->getError());
        }
        $subAdmin = \app\agent\model\AdminModel::where([["admin_id", "=", $input["admin_id"]], ["uniacid", "=", $this->_uniacid]])->findOrEmpty();
        if($subAdmin->isEmpty()) {
            return $this->error("用户不存在");
        }
        if($subAdmin->admin_id == $this->_user["admin_id"]) {
            return $this->error("不可以删除自己");
        }
        $rst = $subAdmin->save(["status" => 0]);
        return $this->success($rst);
    }
    public function bindApp()
    {
        $input = $this->_input;
        $admin_id = $input["admin_id"];
        $modular_id = $input["modular_id"];
        if($admin_id == $this->_user["admin_id"]) {
            return $this->error("超级管理员不能绑定小程序， 您可以创建子账号绑定");
        }
        $admin_bind_count = \app\agent\model\AppAdminModel::where([["admin_id", "=", $admin_id]])->count();
        if(0 < $admin_bind_count) {
            return $this->error("该用户已经绑定了一个小程序");
        }
        $appAdmin = new \app\agent\model\AppAdminModel();
        $rst = $appAdmin->save(["id" => md5($modular_id . $admin_id), "modular_id" => $modular_id, "uniacid" => $this->_uniacid, "admin_id" => $admin_id]);
        return $this->success($rst);
    }
}

?>